﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNQXYJ.Model;
using HNQXYJ.BLL;

namespace HNQXYJ.HN_Terminal
{
    public partial class TerminalUpdateCounty : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetGroups();//首先加载分组列表

                int id =Convert.ToInt32(Request["id"]);
                Terminal tm = TerminalBLL.GetDataById(id);
                this.txtPhone.Text = tm.Phone;
                this.txtTownName.Text = tm.TownName;
                this.ddGroup.SelectedValue = tm.AreaCode; 
                this.txtLeader.Text = tm.Leader;
                this.txtLeaderPhone.Text = tm.LeaderPhone;
                this.txtStatus.Value = tm.Statuss ;
                this.txtLng.Text = tm.Lng.ToString();
                this.txtLat.Text = tm.Lat.ToString();
                this.txtCatgeory.Value =tm.Category;
                this.txtNotes.Text = tm.Notes;
            }
        }

        //更新
        protected void add_Click(object sender, ImageClickEventArgs e)
        {
            Terminal tm = new Terminal();
            int id = Convert.ToInt32(Request["id"]);
            tm.ID = id;           
            tm.Phone = this.txtPhone.Text.Trim();
            tm.TownName = this.txtTownName.Text.Trim();
            tm.AreaCode = this.ddGroup.SelectedValue;
            tm.Leader = this.txtLeader.Text.Trim();
            tm.LeaderPhone = this.txtLeaderPhone.Text.Trim();
            tm.Statuss = this.txtStatus.Value;
            tm.Lng = Convert.ToDouble(this.txtLng.Text.Trim());
            tm.Lat = Convert.ToDouble(this.txtLat.Text.Trim());
            tm.RankLevel = Session["ranklevel"].ToString();
            tm.Category = this.txtCatgeory.Value;
            tm.Notes = this.txtNotes.Text.Trim();

            int res = TerminalBLL.UpdateTerminalById(tm);
            if (res > 0)
            {
                this.Label1.Text = "更新成功！";
            }
            else
            {
                this.Label1.Text = "更新失败！";
            }

        }
        //返回
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("TerminalMgrRankLevel.aspx");
        }

        private void GetGroups()
        {
            string ranklevel = Session["ranklevel"].ToString();
            List<AreaGroup> list = new AreaGroupBLL().GetListByRankLevel(ranklevel);
            if (list != null)
            {
                ddGroup.Items.Add(new ListItem("暂不分组", ranklevel));
                for (int i = 0; i < list.Count; i++)
                {
                    ddGroup.Items.Add(new ListItem(list[i].Name, list[i].AreaCode));
                }
            }
            else
            {
                ddGroup.Items.Add(new ListItem("暂无分组", ranklevel));
            }
        }
    }
}